package com.cn.sinsosft.boot.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * @author wangyupeng
 * @despriction:
 * @ate:Created in 20:38 2019/1/23
 * @modify By:
 */
@Service
@Slf4j
public class UploadService {

    public String upload(MultipartFile fileUpload){
        //获取文件名
        String fileName = fileUpload.getOriginalFilename();
        log.info("文件名{}",fileName);
        //获取文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        log.info("文件名后缀{}",suffixName);
        //重新生成文件名
        fileName = UUID.randomUUID()+suffixName;
        log.info("重新生成文件名{}",fileName);
        //指定本地文件夹存储图片
        String filePath = "D:/static/";
        try {
            //将图片保存到static文件夹里
            fileUpload.transferTo(new File(filePath+fileName));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("fail to upload");
        }
        return "login";
    }
}
